
package com.joysuch.wwyt.mh.service.impl;


import com.joysuch.wwyt.api.alarmbluetooth.dto.OpenApiResult;
import com.joysuch.wwyt.api.alarmbluetooth.service.ReceiveSubscribeWwytService;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.mh.entity.MhAlarmMessage;
import com.joysuch.wwyt.mh.service.CustomAlarmMessageService;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * 调用声光报警器中间件实现类
 *
 * @author smq
 * 2021-10-28 10:38:17
 */
@Slf4j
@Service
public class CustomAlarmMessageServiceImpl implements CustomAlarmMessageService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LocateServerService locateServerService;

    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private ReceiveSubscribeWwytService receiveSubscribeWwytService;


    @Override
    public void send(MhAlarmMessage message, Integer switchType) {

        // 首先判断声光报警蓝牙网关Mac是否配置，如果没有配置则不开启声光报警指令调用
        BaseConfig macConfig = baseConfigService.findConfig(message.getTenentId(), message.getOrgCode(),BaseConfigCodes.OPEN_SERVICE_ALARM_BLUETOOTH_MAC);
        if(macConfig == null || StringUtils.isBlank(macConfig.getValue())){
            return;
        }

        log.info("....调用开放平台蓝牙网关灯光设备控制接口: 开关状态 switchType ={}", switchType);
        try {
            OpenApiResult openApiResult = receiveSubscribeWwytService.receiveSubscribe(message.getTenentId(), message.getOrgCode(),switchType);
            if(openApiResult == null || openApiResult.getErrorCode() != 0){
                log.error("调用开放平台蓝牙网关灯光设备控制接口失败,{}", openApiResult.getErrorMsg()[0]);
            }

        } catch (Exception e) {
            log.error("调用开放平台蓝牙网关灯光设备控制接口失败,{}"+ e.getMessage(),e);
        }
    }

}
